//
//  ChangePassViewController.m
//  ITMS
//
//  Created by 中矿科技有限公司 on 16/7/16.
//  Copyright © 2016年 中矿科技有限公司. All rights reserved.
//

#import "ChangePassViewController.h"
#import "LoginViewController.h"

@interface ChangePassViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;

@end

@implementation ChangePassViewController
{
    AFHTTPData *_http;
    MBProgressHUD *_waitHud;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.automaticallyAdjustsScrollViewInsets =YES;
    self.view.backgroundColor =[UIColor colorWithWhite:0.93 alpha:1];
    self.navigationItem.title = @"修改密码";
    
    [self.passWordNew2 addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.passWordNew1 addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.oldPassWord addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

    _http =[[AFHTTPData alloc]init];
    
    if(!_waitHud)_waitHud=[[MBProgressHUD alloc] initWithView:self.view];//加载
    [self.view addSubview:_waitHud];
}
#pragma mark 监听搜素框输入
-(void)textFieldDidChange:(UITextField *)sender{
    if (_passWordNew1.text.length>0 && ![UITool isBlankString:_passWordNew1.text] && ![UITool isBlankString:_passWordNew2.text]) {
        self.submitBtn.enabled =YES;
        [self.submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }else{
        self.submitBtn.enabled =NO;
        [self.submitBtn setTitleColor:[UIColor colorFromHex:@"CCCCCC"] forState:UIControlStateNormal];
    }
}

- (IBAction)onClicekdSubmit:(UIButton *)sender {
    
    NSString * regex = @"^[A-Za-z0-9]{6,10}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    if (![pred evaluateWithObject:_oldPassWord.text] || ![pred evaluateWithObject:_passWordNew1.text] || ![pred evaluateWithObject:_passWordNew2.text] ) {
        [self showTishiViewWithString:@"密码格式不对"];
        return;
    }
    
    
    if ([UITool isBlankString:_oldPassWord.text] || [UITool isBlankString:_passWordNew2.text] || [UITool isBlankString:_passWordNew1.text]) {
        [MBProgressHUD showSuccess:@"密码不能为空"];
    } else if (![self.passWordNew1.text isEqualToString:self.passWordNew2.text])
    {
        [MBProgressHUD showSuccess:@"新密码不一致，请重新输入"];
        [self.passWordNew2 becomeFirstResponder];
    }else{
        NSMutableDictionary *batchDict =[[NSMutableDictionary alloc]init];
        [batchDict setObject:_oldPassWord.text forKey:@"currentPassword"];
        [batchDict setObject:_passWordNew2.text forKey:@"newPassword"];
        [_http postTokenDataWithUrl:SAPChangePasswordKUrl httpBobyData:batchDict onSuccess:^(NSData *data) {
            NSDictionary *dict =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"%@",dict);
            if ([dict[@"success"] integerValue] ==1) {
                [MBProgressHUD showSuccess:@"密码修改成功"];
                [UserManager sharedInstance].lastConnectionStatus =ZYDConnectionStatusMannualLogout;
                [[AppDelegate sharedAppDelegate] enterLoginUI];
            }else if([dict[@"error"] isKindOfClass:[NSDictionary class]]){
                [MBProgressHUD showSuccess:dict[@"error"][@"message"]];
            }

        } onFailure:^(NSError *error) {
            [MBProgressHUD showSuccess:@"密码修改失败"];
        }];
    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)onClicekdOldPassBtn:(UIButton *)sender {
    sender.selected =!sender.selected;
    if (sender.selected) {
        self.oldPassWord.secureTextEntry =NO;
    }else{
        self.oldPassWord.secureTextEntry =YES;
    }

}
- (IBAction)onClickedNewPass1Btn:(UIButton *)sender {
    sender.selected =!sender.selected;
    if (sender.selected) {
        self.passWordNew1.secureTextEntry =NO;
    }else{
        self.passWordNew1.secureTextEntry =YES;
    }

}
- (IBAction)onClickedNewPass2Btn:(UIButton *)sender {
    sender.selected =!sender.selected;
    if (sender.selected) {
        self.passWordNew2.secureTextEntry =NO;
    }else{
        self.passWordNew2.secureTextEntry =YES;
    }

}


#pragma mark- funcs
- (void)showTishiViewWithString:(NSString*)tishi{
    _waitHud.label.text = tishi;
    _waitHud.mode = MBProgressHUDModeText;
    [_waitHud showAnimated:YES];
    [_waitHud hideAnimated:YES afterDelay:2];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
